package com.hdu.point_offer;

public class lc172 {
    class Solution {
        public int countTarget(int[] scores, int target) {
            int l = find(scores, target, true);
            int r = find(scores, target, false);
            if (l == -1 && r == -1)
            {
                return 0;
            }
            return r - l + 1;
        }

        int find(int[] scores, int target, boolean findFirst)
        {
            int l = 0, r = scores.length - 1, ans = -1;
            while (l <= r)
            {
                int mid = l + ((r - l) >> 1);
                if (scores[mid] == target)
                {
                    if (findFirst)
                    {
                        ans = mid;
                        r = mid - 1;
                    }
                    else
                    {
                        ans = mid;
                        l = mid + 1;
                    }
                }
                else if (scores[mid] > target)
                {
                    r = mid - 1;
                }
                else
                {
                    l = mid + 1;
                }
            }
            return ans;
        }
    }
}
